<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                   
                    <el-form-item prop="nickname" label="昵称">
                        <el-input
                            v-model="baTable.form.items!.nickname"
                            type="string"
                            :placeholder="t('Please input field', { field: '昵称' })"
                        ></el-input>
                    </el-form-item>
                    <FormItem label="头像" type="image" v-model="baTable.form.items!.avatar" :input-attr="{
                        hideSelectFile:true}"  />
                    <el-form-item prop="email" label="邮箱">
                        <el-input
                            v-model="baTable.form.items!.email"
                            type="string"
                            :placeholder="t('Please input field', { field: '邮箱' })"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="mobile" label="手机号">
                        <el-input
                            v-model="baTable.form.items!.mobile"
                            type="string"
                            :placeholder="t('Please input field', { field: '手机号' })"
                        ></el-input>
                    </el-form-item>
                    <FormItem
                        label="性别"
                        v-model="baTable.form.items!.gender"
                        type="radio"
                        :data="{
                            content: { 0: '未知', 1: '男', 2: '女' },
                            childrenAttr: { border: true },
                        }"
                    />
                    <FormItem
                        label="会员"
                        v-model="baTable.form.items!.user_type"
                        type="radio"
                        :data="{
                            content: { 0: '普通用户', 1: '会员用户', 2: '全站VIP用户' },
                            childrenAttr: { border: true },
                        }"
                    />
                    <el-form-item label="生日">
                        <el-date-picker
                            class="w100"
                            value-format="YYYY-MM-DD"
                            v-model="baTable.form.items!.birthday"
                            type="date"
                            :placeholder="t('Please select field', { field: '出生日期' })"
                        />
                    </el-form-item>
                    <el-form-item label="会员充值时间" v-if="baTable.form.operate == 'Edit' && baTable.form.items!.user_type > 0">
                        <el-date-picker
                            class="w100"
                            value-format="YYYY-MM-DD"
                            v-model="baTable.form.items!.recharge_time"
                            type="date"
                            :placeholder="t('Please select field', { field: '会员充值时间' })"
                        />
                    </el-form-item>
                    
                    <el-form-item label="会员到期时间" v-if="baTable.form.items!.user_type > 0">
                        <el-date-picker
                            class="w100"
                            value-format="YYYY-MM-DD"
                            v-model="baTable.form.items!.expiration_time"
                            type="date"
                            :placeholder="t('Please select field', { field: '会员到期时间' })"
                        />
                    </el-form-item>
                    <el-form-item prop="password" label="密码">
                        <el-input
                            v-model="baTable.form.items!.password"
                            type="password"
                            :placeholder="
                                baTable.form.operate == 'Add'
                                    ? t('Please input field', { field: '用户密码不输入则使用默认密码' })
                                    : '不修改请留空'
                            "
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="motto" label="签名">
                        <el-input
                            @keyup.enter.stop=""
                            @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                            v-model="baTable.form.items!.motto"
                            type="textarea"
                            :placeholder="t('Please input field', { field: '用户签名' })"
                        ></el-input>
                    </el-form-item>
                    <FormItem
                        label="状态"
                        v-model="baTable.form.items!.status"
                        type="radio"
                        :data="{ content: { '0': '禁用', '1': '开启' }, childrenAttr: { border: true } }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import { regularPassword } from '/@/utils/validate'
import type { ElForm, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import router from '/@/router/index'
import { buildValidatorData } from '/@/utils/validate'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    nickname: [buildValidatorData({ name: 'required', title: t('user.user.nickname') })],
    email: [buildValidatorData({ name: 'email', title: t('user.user.email') })],
    mobile: [buildValidatorData({ name: 'mobile' })],
    password: [
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (baTable.form.operate == 'add') {
                    if (!val) {
                        return callback(new Error(t('Please input field', { field: t('user.user.password') })))
                    }
                } else {
                    if (!val) {
                        return callback()
                    }
                }
                if (!regularPassword(val)) {
                    return callback(new Error(t('validate.Please enter the correct password')))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
})

const changeAccount = (type: string) => {
    baTable.toggleForm()
    router.push({
        name: type == 'money' ? 'user/moneyLog' : 'user/scoreLog',
        query: {
            user_id: baTable.form.items!.id,
        },
    })
}

watch(
    () => baTable.form.operate,
    (newVal) => {
        rules.password![0].required = newVal == 'add'
    }
)
</script>

<style scoped lang="scss">
.avatar-uploader {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: var(--el-border-radius-small);
    box-shadow: var(--el-box-shadow-light);
    border: 1px dashed var(--el-border-color);
    cursor: pointer;
    overflow: hidden;
    width: 110px;
    height: 110px;
}
.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}
.avatar {
    width: 110px;
    height: 110px;
    display: block;
}
.image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
</style>
